package cn.edu.scau.vsac.controller;

import cn.edu.scau.vsac.common.Constants;
import cn.edu.scau.vsac.common.ResultData;
import cn.edu.scau.vsac.domain.dto.HelpRequestDto;
import cn.edu.scau.vsac.entity.HelpRequest;
import cn.edu.scau.vsac.service.HelpRequestService;
import cn.edu.scau.vsac.util.HostHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("RequestHelp")
@Tag(name = "请求帮扶")
public class HelpRequestController {
    @Autowired
    HelpRequestService helpRequestService;
	@Autowired
    HostHolder hostHolder;
    @Operation(summary = "新增帮扶请求")
    @RequestMapping(path = "/create", method = RequestMethod.POST)
    public ResultData<HelpRequest> create(@RequestBody HelpRequestDto helpRequestDto) {
        HelpRequest helpRequest = helpRequestService.create(helpRequestDto);
        if (helpRequest == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(helpRequest);
    }

    @Operation(summary = "根据主键删除帮扶请求", description = "根据主键删除帮扶请求")
    @RequestMapping(path = "/deleteById/{id}", method = RequestMethod.POST)
    public ResultData<String> deleteById(@PathVariable Long id) {
        String delete = helpRequestService.deleteById(id);
        if (delete == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(delete);
    }

    @Operation(summary = "根据主键查询帮扶请求")
    @RequestMapping(path = "/getOneById/{id}", method = RequestMethod.GET)
    public ResultData<HelpRequest> getOneById(@PathVariable Long id) {
        HelpRequest helpRequest = helpRequestService.getOneById(id);
        if (helpRequest == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(helpRequest);
    }

    @Operation(summary = "根据求助人id查询帮扶请求", description = "传入用户id和分页信息 根据求助人id查询帮扶请求")
    @RequestMapping(path = "/getPageByUserId", method = RequestMethod.GET)
    public ResultData<List<HelpRequest>> getPageByUserId() {
        Long userId = hostHolder.getUser().getUserId();
        List<HelpRequest> res = helpRequestService.getPageByUserId(userId);
        if (res == null) {
            return ResultData.fail(Constants.ResponseCode.INTERNAL_SERVER_ERROR);
        }
        return ResultData.success(res);
    }

    @Operation(summary = "查询出所有帮扶请求")
    @RequestMapping(path = "/getAll", method = RequestMethod.GET)
    public ResultData<List<HelpRequest>> getAll() {
        List<HelpRequest> res = helpRequestService.list();
        return ResultData.success(res);
    }


}
